package com.han.hotelplat.comm.utils;

import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;

import javax.persistence.Entity;
import org.mozilla.intl.chardet.nsDetector;
import org.mozilla.intl.chardet.nsICharsetDetectionObserver;
import org.mozilla.intl.chardet.nsPSMDetector;

/**
 * @date    2013-11-25 下午05:20:45 
 * @author  xiaohan
 * @version 1.0.0
 */

@Entity
public class CharsetDetector {
	 private static boolean found = false;
	    private static String result;
	    private static int lang;

	    public static String[] detectChineseCharset(InputStream in) throws IOException
	    {
	        lang = nsPSMDetector.CHINESE;
	        String[] prob;
	        // Initalize the nsDetector() ;
	        nsDetector det = new nsDetector(lang);
	        // Set an observer...
	        // The Notify() will be called when a matching charset is found.

	        det.Init(new nsICharsetDetectionObserver()
	        {

	            @Override
				public void Notify(String charset)
	            {
	                found = true;
	                result = charset;
	            }
	        });
	        BufferedInputStream imp = new BufferedInputStream(in);
	        byte[] buf = new byte[1024];
	        int len;
	        boolean isAscii = true;
	        while ((len = imp.read(buf, 0, buf.length)) != -1)
	        {
	            // Check if the stream is only ascii.
	            if (isAscii)
	                isAscii = det.isAscii(buf, len);
	            // DoIt if non-ascii and not done yet.
	            if (!isAscii)
	            {
	                if (det.DoIt(buf, len, false))
	                    break;
	            }
	        }
	        imp.close();
	        in.close();
	        det.DataEnd();
	        if (isAscii)
	        {
	            found = true;
	            prob = new String[]
	                    {
	                        "ASCII"
	                    };
	        } else if (found)
	        {
	            prob = new String[]
	                    {
	                        result
	                    };
	        } else
	        {
	            prob = det.getProbableCharsets();
	        }
	        return prob;
	        }

}
